package com.feinno.bug.actcache.controller;

import com.feinno.bug.actcache.help.PasswordType;
import com.feinno.bug.actcache.help.RandomPSWd;
import com.feinno.bug.actcache.service.TaskService;
import com.feinno.bug.actcache.service.impl.Task;
import com.feinno.bug.actcache.util.encrypt.AESUtil;
import com.feinno.bug.actcache.util.encrypt.ParseSystemUtil;
import com.feinno.bug.actcache.util.encrypt.RSAUtils;
import com.feinno.bug.actcache.util.http.HttpApacheClientUtil;
import com.feinno.bug.actcache.vo.MessageVo;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zcy
 * @Description
 * @Date: 19:21 2018/11/2
 */
@RestController
public class
BugController {

    @Autowired
    private TaskService taskService;

    @Value("${platform.publicKey}")
    private String platform_publicKey;

    @Value("${platform.privateKey}")
    private String platform_privateKey;

    @Value("${access.publicKey}")
    private String access_publicKey;

    @Value("${access.privateKey}")
    private String access_privateKey;

    @RequestMapping(value = "/bug", method = RequestMethod.GET)
    public void test(HttpServletRequest request) throws InterruptedException {
        Integer flag = Integer.valueOf(request.getParameter("flag"));
        taskService.mytask(flag);
    }

    @RequestMapping(value = "/server/encrypt", method = RequestMethod.POST)
    public void testencrypt(@RequestParam(name = "content") String content, HttpServletRequest request) {
//        String content = messageVo.getContent();
        // 请求头中加密的aes_key
        String aes_key = request.getHeader("aes-key");
        try {
            // 解密之后的字符串
            String decryptAesKey = RSAUtils.privateDecrypt(aes_key, RSAUtils.getPrivateKey(platform_privateKey));
            byte[] bytes = ParseSystemUtil.parseHexStr2Byte(content);
            // 解密之后的请求体
            byte[] decrypt = AESUtil.decrypt(bytes, decryptAesKey);
            String reqBody = new String(decrypt);
            System.out.println("请求体:" + reqBody);
            // 处理请求体
            // 随机生成aes-key
            // 使用aes-key加密响应体
            // 使用access_publicKey加密aes-key，放到响应头中
        } catch (NoSuchAlgorithmException e) {
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping(value = "/client/encrypt", method = RequestMethod.POST)
    public void clientEncrypt(@RequestBody MessageVo messageVo, HttpServletRequest request) {
        String aesKey = (new RandomPSWd()).GernaratePWD(PasswordType.Mixed, 16);
        Gson gson = new Gson();
        String data = gson.toJson(messageVo);
        // 加密的请求体
        byte[] encrypt = AESUtil.encrypt(data, aesKey);
        // 加密的请求体字符串
        String content = ParseSystemUtil.parseByte2HexStr(encrypt);
        try {
            // 加密之后的aesKey
            String encryptAESKey = RSAUtils.publicEncrypt(aesKey, RSAUtils.getPublicKey(platform_publicKey));
            Map<String, String> map = new HashMap<>();
            map.put("Content-Type", "application/json;encoding=utf-8");
            map.put("aes-key", encryptAESKey);
            HttpResponse httpResponse = HttpApacheClientUtil.httpPost("http://127.0.0.1:8080/server/encrypt?content=" + content, map, null);
            // 响应的header头
            Header firstHeader = httpResponse.getFirstHeader("aes-key");
            // 加密的响应头
            String value = firstHeader.getValue();
            //使用access_privateKey解密aes-key，得到aes-key
            String aes_key = RSAUtils.privateDecrypt(value, RSAUtils.getPrivateKey(access_privateKey));
            // 响应体的密文
            String responseStr = HttpApacheClientUtil.getResponseStr(httpResponse);
            byte[] bytes = ParseSystemUtil.parseHexStr2Byte(responseStr);
            // 响应体的明文
            String respBody = new String(AESUtil.decrypt(bytes, aes_key));
            System.out.println(respBody);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
